<template>
  <div class="user-manage-container">
    <el-card class="content">
      <div class="header">
        <!-- 导入 -->
        <el-button
          v-permission="['importUser']"
          @click="onImportExcel"
          type="primary"
        >
          {{ $t('msg.excel.importExcel') }}</el-button
        >
        <!-- 导出 -->
        <el-button type="success" @click="onToExcelClick">
          {{ $t('msg.excel.exportExcel') }}
        </el-button>
      </div>
    </el-card>
    <el-card>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column
          align="center"
          type="index"
          label="#"
          width="50"
        ></el-table-column>
        <!-- 用户名 -->
        <el-table-column prop="username" :label="$t('msg.excel.name')">
        </el-table-column>
        <!-- 手机号 -->
        <el-table-column prop="mobile" :label="$t('msg.excel.mobile')">
        </el-table-column>
        <!-- 头像 -->
        <el-table-column prop="mobile" :label="$t('msg.excel.avatar')">
          <template v-slot="{ row }">
            <el-image
              class="avatar"
              :src="row.avatar"
              :preview-src-list="[row.avatar]"
            >
            </el-image>
          </template>
        </el-table-column>
        <!-- 角色 -->
        <el-table-column :label="$t('msg.excel.role')">
          <template v-slot="{ row }">
            <div v-if="row.role && row.role.length > 0">
              <el-tag v-for="item in row.role" :key="item.id" size="mini">{{
                item.title
              }}</el-tag>
            </div>
            <div v-else>
              <el-tag size="mini">{{ $t('msg.excel.defaultRole') }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column :label="$t('msg.excel.openTime')" prop="openTime">
          <template #default="{ row }">
            {{ $filters.dateFilter(row.openTime) }}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
          :label="$t('msg.excel.action')"
          fixed="right"
          width="260"
        >
          <template #default="{ row }">
            <!-- 查看 -->
            <el-button
              @click="onShowClick(row._id)"
              type="primary"
              size="mini"
              >{{ $t('msg.excel.show') }}</el-button
            >
            <!-- 角色 -->
            <el-button
              v-permission="['distributeRole']"
              @click="showRole(row)"
              type="info"
              size="mini"
              >{{ $t('msg.excel.showRole') }}</el-button
            >
            <el-button
              v-permission="['removeUser']"
              @click="onRemoveClick(row)"
              type="danger"
              size="mini"
              >{{ $t('msg.excel.remove') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        class="pagination"
        :current-page="page"
        :page-size="size"
        :total="total"
        :page-sizes="[1, 2, 5, 10]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-card>
    <ExportToExcel v-model="isExportExcel" />
    <Roles
      v-model="roleDialogVisible"
      @updateRole="getListData"
      :userId="curUserId"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, onActivated, watch } from 'vue'
import { getUserMangeList, deleteUser } from '@/api/user-manage'
import { watchSwitchLang } from '@/utils/i18n'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useI18n } from 'vue-i18n'
import ExportToExcel from './components/Export2Excel.vue'
import Roles from './components/roles.vue'
const i18n = useI18n()
const router = useRouter()
const roleDialogVisible = ref(false)
const curUserId = ref('')
watch(roleDialogVisible, (cur) => {
  if (!cur) curUserId.value = ''
})
// 点击角色按钮
const showRole = (row) => {
  curUserId.value = row._id
  roleDialogVisible.value = true
}
// 点击查看
const onShowClick = (id) => {
  router.push(`/user/info/${id}`)
}

// 删除
const onRemoveClick = (row) => {
  ElMessageBox.confirm(
    i18n.t('msg.excel.dialogTitle1') +
      row.username +
      i18n.t('msg.excel.dialogTitle2'),
    {
      type: 'warning'
    }
  ).then(async () => {
    await deleteUser(row._id)
    ElMessage.success(i18n.t('msg.excel.removeSuccess'))
    // 重新渲染数据
    getListData()
  })
}
// 存储列表数据
const tableData = ref([])
const size = ref(3)
const page = ref(1)
const total = ref(0)
const getListData = async () => {
  console.log(page.value, size.value)
  const res = await getUserMangeList({
    page: page.value,
    size: size.value
  })
  tableData.value = res.list
  total.value = res.total - 1
}
// 页数变化
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData()
}
// 页码变化
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData()
}
onMounted(getListData)
onActivated(getListData)
watchSwitchLang(getListData)

// 导入按钮
const onImportExcel = () => {
  router.push('/user/import')
}
// 导出相关
const isExportExcel = ref(false)
const onToExcelClick = () => {
  isExportExcel.value = true
}
</script>

<style lang="scss" scoped>
.user-manage-container {
  .content {
    margin-bottom: 20px;
    .header {
      text-align: center;
    }
  }

  ::v-deep .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  ::v-deep .el-tag {
    margin-right: 6px;
  }

  .pagination {
    margin-top: 20px;
    text-align: center;
  }
}
</style>
